################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

# Description:

## Code to make Figure A.10: "Committee representation"
################################################################################


# Packages

library(data.table)
library(dplyr)
library(ggplot2)
library(broom)
library(showtext)
library(purrr)

# Directories (wd is set by master.R)

dir      <-  "../data/2_processed_data"
fig.dir  <-  "../results/figures"

# data

d <- fread(paste(dir, "speeches_session_lemma.csv", sep = "/"))

# data cleaning

d[, policy_committee_list   := str_split(policy_com, "\\|")]
d[, `:=` (Old   = as.numeric(age_cat == "old"),
          Urban = as.numeric(town == "urban"),
          White_Collar = as.numeric(occupation == "white"),
          Female = female)]
d.plot <- d[, .(policy_com = unlist(policy_committee_list)), 
            by = c("pid_session", "Female", "Old", "Urban", "White_Collar")]
d.plot[, policy_com := str_trim(policy_com)]

# Analysis

yvars <- c("Female", "Old", "Urban", "White_Collar")

# Loop over them and run the same model
results <- map_dfr(yvars, function(yvar) {
  model <- lm(as.formula(paste0(yvar, " ~ -1 + as.factor(policy_com)")), data = d.plot)
  
  tidy(model, conf.int = TRUE) %>%
    select(term, estimate, conf.low, conf.high) %>%
    mutate(
      term = str_remove(term, "^as\\.factor\\(.*?\\)"),
      mean.yvar = mean(d.plot[[yvar]], na.rm = TRUE),
      outcome = yvar
    )
})


# Results

results <- results |>
  mutate(outcome = case_when(outcome == "White_Collar" ~ "White-Collar", T ~ outcome),
         term = factor(term, levels = rev(sort(unique(term)))))

# Plotting

p <- results |>  
  ggplot(aes(x = estimate, y = term, group = outcome)) +
  facet_wrap(~ as.factor(outcome), scales = "fixed", axes = "all_x") +
  geom_errorbar(aes(xmin = conf.low, xmax = conf.high,width = 0.1)) +
  geom_point(size = 2, color = "black") +
  geom_vline(
    data = distinct(results, outcome, mean.yvar),
    aes(xintercept = mean.yvar),
    linetype = 3
  ) +
  theme(
    panel.grid = element_blank(),
    panel.border = element_blank(),
    panel.grid.major.y= element_line(linewidth = 0.02, colour = "grey90"),
    panel.background = element_rect(fill = "transparent"),
    plot.background = element_rect(fill = "transparent"),
    plot.title = element_text(family = "Montserrat", color = "black", size = 20, hjust = 0.5, margin=margin(0,0,30,0),),
    panel.grid.major.x = element_blank(),
    strip.text = element_text(family = "Montserrat", color = "black", size = 14),
    strip.background = element_blank(),
    axis.ticks.length = unit(0, "lines"),
    axis.text.y = element_text(family = "Montserrat", size = 10),
    axis.title.y = element_text(family = "Montserrat", size = 13, margin=margin(0,30,0,0)),
    axis.text.x = element_text(family = "Montserrat", size = 12, angle = 45, hjust = 1),
    axis.title.x = element_text(family = "Montserrat", size = 13, margin=margin(30,0,0,0)),
    panel.spacing = unit(2, "lines")
  ) +
  scale_x_continuous(breaks = seq(0, 1, .25)) +
  coord_cartesian(xlim = c(0, 1)) +
  labs(title = "", x = "", y = "")


ggsave(paste(fig.dir, "figA10.pdf", sep = "/"), width = 12.5, height = 12, pointsize = 12)








